package ru.spb.osll.myfit.server.chart;

import org.apache.log4j.Logger;
import ru.spb.osll.myfit.util.parser.ILogParser;
import ru.spb.osll.myfit.util.parser.IResultSection;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * User: Artem Bukhonov
 * Date: 21.04.12
 * Time: 19:53
 */
public class LogReader {
  public static final Logger log = Logger.getLogger(LogReader.class);

  public List<? extends IResultSection> read(String fileName, ILogParser parser) throws IOException {
    BufferedReader bufferedReader = null;
    try {
      bufferedReader = new BufferedReader(new FileReader(fileName));
      String s;
      while ((s = bufferedReader.readLine()) != null) {
        if (s.contains("INFO")) {
          parser.infoLine(s);
        }
        else if (s.contains("ERROR")) {
          parser.errorLine(s);
        }
      }
    } catch (IOException e) {
      log.error(e.getMessage());
      throw e;
    }finally{
      if(bufferedReader != null) {
        try {
          bufferedReader.close();
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
    }
    return parser.getResults();
  }
}
